C++ MACROS learn by doing
Table of Content
Macros are section of text that replace by the compiler preprocessor stage, it’s very useful in DEBUGGING for example
Demo#
Control with macro to use
main.cpp
#include <iostream>
#if MY_DEBUG == 1
#define LOG(x) std::cout << x << std::endl;
#else
#define LOG(x)
#endif
int main() {
LOG("HELLO")
}
build and run#
terminal
g++ main.cpp -o main -DMY_DEBUG=0
./main
# print nothing
g++ main.cpp -o main -DMY_DEBUG=1
./main
HELLO
CMakeLists.txt#
cmake_minimum_required(VERSION 3.1)
project(demo_macros)
add_definitions(-DMY_DEBUG=0)
add_executable(main main.cpp)
CMakeLists with options#
cmake_minimum_required(VERSION 3.1)
project(demo_macros)
option(USE_DEBUG "Enter debug mode" OFF)
if (USE_DEBUG)
add_definitions(-DMY_DEBUG=1)
endif()
add_executable(main main.cpp)
usage#
options
cmake -LA ..
#
...
CMAKE_STRIP:FILEPATH=/usr/bin/strip
CMAKE_VERBOSE_MAKEFILE:BOOL=FALSE
USE_DEBUG:BOOL=OFF
#
cmake -DUSE_DEBUG=ON ..